﻿using System;
using System.Linq;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using System.Web.UI.WebControls;
//Sarah Ehlers This is the logic behind the main part of the SendMessage

namespace CSC440TeamOne.Admin.Views
{
    public partial class SendMessage : Microsoft.Practices.CompositeWeb.Web.UI.Page, ISendMessageView
    {
        private SendMessagePresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public SendMessagePresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            RepUsers.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(RepUsers_ItemDataBound);
            RepUsers.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(RepUsers_ItemCommand);
        }

        protected IDictionary<string, string> Users
        {
            get
            {
                IDictionary<string,string> result = new Dictionary<string, string>();
                var obgResult = ViewState["Users"];
                if (obgResult != null)
                {
                    result = obgResult as IDictionary<string, string>;
                }
                return result;
            }
            set { ViewState["Users"] = value; }
        }



        void RepUsers_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            var UserId = e.CommandArgument;
            var email = Users[UserId.ToString()];
            txtTo.Text += ", " + email;

        }

        void RepUsers_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, Common.User>.Bind(e.Item, a =>
            {
                a.Find<LinkButton>("btnUser").Text = a.BusinessItem.FirstName;
                a.Find<LinkButton>("btnUser").CommandArgument = a.BusinessItem.UserId.ToString();
            });
        }
        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	
        public void show(Common.SendMessageModel model)
        {
            IEnumerable<Common.User> users = model.users;

            if (users.Any())
            {
                RepUsers.DataSource = users;
                RepUsers.DataBind();
                Users = new Dictionary<string, string>();
                foreach (var user in users)
                {

                    Users.Add(user.UserId.ToString(), user.EmailAddress);
                }
            }
        }
    }
}

